package service

import (
	"context"
	"gitee.com/llakcs/agile-go/registry"
	"gitee.com/llakcs/agile-go/registry/nacos"
)

// nacos 服务发现与注册
func DiscoveryAndRegNacos() error {
	return reg()
}

// 注册
func reg() error {
	confs := make([]nacos.ServerConfig, 0)
	service := &registry.ServiceInstance{
		ID:      "123aaabbbccddd",
		Name:    "agile-layout-nacos-service",
		Version: "1.0",
		//Metadata:  map[string]string{"idc": "shanghai-xs"},
		Endpoints: []string{"http://127.0.0.1:8080?isSecure=false"},
	}

	conf := nacos.ServerConfig{
		IpAddr: "127.0.0.1",
		Port:   18848,
	}
	confs = append(confs, conf)
	r, err := nacos.NewRegistry(
		nacos.WithUserName("nacos"),
		nacos.WithPassWord("nacos"),
		nacos.WithWeight(10),
		nacos.WithNameSpaceId("test"),
		nacos.WithServerConfs(confs),
		nacos.WithNotLoadCacheAtStart(true),
	)
	if err != nil {
		return err
	}
	e := r.Register(context.Background(), service)
	if e != nil {
		return e
	}
	//w, e2 := r.Watch(context.Background(), service.Name)
	//if e2 != nil {
	//	log.Info("watcher err:", e2)
	//}
	return nil
}

//// 监听
//func watcher() error {
//
//}
